import 'dart:io';

void main() {
  List<Map<String, double>> incomeList = [];
  List<Map<String, double>> expenseList = [];

  while (true) {
    print("\nBudget Management Application");
    print("1. Add Income");
    print("2. Add Expense");
    print("3. View Summary");
    print("4. Exit");
    stdout.write("Choose an option: ");
    String? choice = stdin.readLineSync();

    switch (choice) {
      case '1':
        addEntry(incomeList, "Income");
        break;
      case '2':
        addEntry(expenseList, "Expense");
        break;
      case '3':
        viewSummary(incomeList, expenseList);
        break;
      case '4':
        print("Exiting application. Goodbye!");
        return;
      default:
        print("Invalid choice. Please try again.");
    }
  }
}

void addEntry(List<Map<String, double>> list, String type) {
  stdout.write("Enter $type description: ");
  String? description = stdin.readLineSync();

  stdout.write("Enter $type amount: ");
  String? amountInput = stdin.readLineSync();
  double? amount = double.tryParse(amountInput ?? "");

  if (amount == null || amount < 0) {
    print("Invalid amount. Please enter a positive number.");
    return;
  }

  list.add({description ?? "Unknown": amount});
  print("$type added successfully.");
}

void viewSummary(List<Map<String, double>> incomeList, List<Map<String, double>> expenseList) {
  double totalIncome = incomeList.fold(0, (sum, item) => sum + item.values.first);
  double totalExpense = expenseList.fold(0, (sum, item) => sum + item.values.first);
  double netProfit = totalIncome - totalExpense;

  print("\n--- Budget Summary ---");
  print("Total Income: \$${totalIncome.toStringAsFixed(2)}");
  print("Total Expenses: \$${totalExpense.toStringAsFixed(2)}");
  print("Net ${netProfit >= 0 ? 'Profit' : 'Loss'}: \$${netProfit.abs().toStringAsFixed(2)}");

  if (netProfit > 0) {
    double tax = netProfit * 0.15;
    print("Tax to be paid (15%): \$${tax.toStringAsFixed(2)}");
  }
}